/**
 * @file LayerManager.cpp
 *
 * @date 12/02/2012
 * @author Tales 
 *
 */

#include "LayerManager.hpp"
#include "ActorManager.hpp"
#include "errors.hpp"
#include "Layer.hpp"

namespace wolf {

LayerManager *LayerManager::instance = NULL;

static bool layerComp(Layer* lhs, Layer* rhs) {
	if (lhs->getDepth() > rhs->getDepth())
		return true;
	else if (lhs->getDepth() < rhs->getDepth())
		return false;
	else
		return lhs < rhs;
}

LayerManager::LayerManager() :
		layers(layerComp) {
	if (instance)
		throw SingletonError("Singleton violation error on Layer Manager");
	instance = this;
}

LayerManager::~LayerManager() {
	instance = NULL;
}

LayerManager & LayerManager::getInstance() {
	return ActorManager::getInstance();
}

void LayerManager::onDraw(Engine & engine, SDL_Surface *dst) {
	WOLF_FOR( set<Layer*>, layers , it ) {
		(*it)->draw(dst);
	}
}

void LayerManager::addLayer(Layer & layer) {
	layers.insert(&layer);
}

void LayerManager::removeLayer(Layer & layer) {
	layers.erase(&layer);
}

bool LayerManager::hasLayer(Layer & layer) {
	return layers.count(&layer);
}

} /* namespace wolf */
